package com.book.modules.helper;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.book.common.api.entity.CurrentUser;
import com.book.oauth.server.user.vo.CurrentUserDetail;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * Description：
 * @Author： leo.xiong
 * @CreateDate： 2020/04/21 17:06
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@Slf4j
@Component
public class ExcelHelper {

    @Autowired
    private CurrentUserHelper currentUserHelper;

    public static final String XLSX = ".xlsx";

    public static final String XLS = ".xls";

    public static final String CSV = ".csv";

    public <T> void exportExcel(String fileName, Class<T> clazz) {
        exportExcel(fileName, fileName, fileName, fileName, XLSX, clazz, Lists.newArrayList());
    }

    public <T> void exportExcel(String fileName, Class<T> clazz, List<T> list) {
        exportExcel(fileName, fileName, fileName, fileName, XLSX, clazz, list);
    }

    public <T> void exportExcel(String title, String fileName, Class<T> clazz, List<T> list) {
        exportExcel(title, title, title, fileName, XLSX, clazz, list);
    }

    public <T> void exportExcel(String title, String secondTitle, String sheetName, String fileName, Class<T> clazz, List<T> list) {
        exportExcel(title, secondTitle, sheetName, fileName, XLSX, clazz, list);
    }

    /**
     * 导出Excel
     *
     * @param title
     * @param secondTitle
     * @param sheetName
     * @param fileName
     * @param suffix
     * @param clazz
     * @param list
     * @param <T>
     */
    public <T> void exportExcel(String title, String secondTitle, String sheetName, String fileName, String suffix, Class<T> clazz, List<T> list) {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, secondTitle, sheetName), clazz, list);
        CurrentUser currentUser = currentUserHelper.getCurrentUser();
        if (currentUser == null) {
            return;
        }
        String filePath = currentUser.getId() + File.separator + System.currentTimeMillis() + File.separator + fileName;
        FileOutputStream outputStream = null;
        try {
            File tempFile = File.createTempFile(filePath, suffix);
            tempFile.deleteOnExit();
            outputStream = new FileOutputStream(tempFile.getAbsoluteFile());
            workbook.write(outputStream);
        } catch (IOException e) {
            log.error("写入数据异常", e);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    log.error("关闭流异常", e);
                }
            }
        }
    }

    /**
     * 导入EXCEL
     *
     * @param filePath
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> List<T> importExcel(String filePath, Class<T> clazz) {
        return ExcelImportUtil.importExcel(new File(filePath), clazz, new ImportParams());
    }
}
